package com.wjc.train.generator.generators;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.sql.Types;
import java.util.Collections;

/**
 * @author Guessed
 */
public class MPGenerator {

    private static final String URL= "jdbc:mysql://localhost:3306/train_business?serverTimezone=Asia/Shanghai&characterEncoding=utf-8&allowMultiQueries=true&remarks=true&useInformationSchema=true";
    private static final String USER_NAME="train_business";
    private static final String USER_PWD="train.business";

    public static String LOWER_ENTITY;
    public static final String TABLE_NAME="sk_token";
    public static final String MODULE_NAME="business";

    public static void main(String[] args) {
        FastAutoGenerator.create(URL, USER_NAME, USER_PWD)
                .globalConfig(builder -> {
                    builder.author("Guessed") // 设置作者
                            //.enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .outputDir("D:\\JavaProject\\train\\business\\src\\main\\java"); // 指定输出目录
                })
                .dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                    int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                    if (typeCode == Types.SMALLINT) {
                        // 自定义类型转换
                        return DbColumnType.INTEGER;
                    }
                    return typeRegistry.getColumnType(metaInfo);

                }))
                .packageConfig(builder -> {
                    builder.parent("com.wjc.train.business") // 设置父包名
//                            .moduleName("member") // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.xml, "D:\\JavaProject\\train\\business\\src\\main\\resources\\mapper")); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.controllerBuilder().enableRestStyle();
                    builder.entityBuilder().addIgnoreColumns("create_time","update_time","is_deleted");
                    builder.addInclude(TABLE_NAME) // 设置需要生成的表名
                            .addTablePrefix("t_", "c_");// 设置过滤表前缀

                }).injectionConfig(x->{
                    x.beforeOutputFile((tableInfo, objectMap) -> {
                        System.out.println("tableInfo: " + tableInfo.getEntityName() + " objectMap: " + objectMap.size());
                        LOWER_ENTITY=tableInfo.getEntityName().toLowerCase();
                        objectMap.put("lowerEntity", LOWER_ENTITY);
                        objectMap.put("moduleName", MODULE_NAME);
                    });
                    x.customFile(Collections.singletonMap("Repository.java", "/templates/repository.java.ftl"));
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}
